﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.Syndication;
using System.IO;
using System.Xml;

namespace NBA_Feed.BaseClass
{
    public class HttpLogic
    {
        WebClient _webClient = new WebClient();

        public void GetRssFeed(string url, Action<SyndicationFeed> action)
        {
            _webClient.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Error != null)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show(e.Error.Message);
                    });
                }
                else
                {
                    StringReader stringReader = new StringReader(e.Result);
                    XmlReader xmlReader = XmlReader.Create(stringReader);
                    SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        action(feed);
                    });
                }
            };

            _webClient.DownloadStringAsync(new Uri(url));
        }
    }
}
